/**
 * Created by Administrator on 2017/3/26 0026.
 */
layui.use(['form', 'layedit'], function() { //独立版的layer无需执行这一句
    var $ = layui.jquery, layer = layui.layer,  form = layui.form(), layedit = layui.layedit;
    $(function () {
        //监听提交
        form.on('submit(send_form)', function(data){
            var _this = $(data.form);
            $.ajax({
                type: _this.attr('method'),
                url: _this.attr('action'),
                data: _this.serializeArray(),
                dataType: 'json',
                success: function (msg) {
                    if (msg.code == 200) {
                        layer.alert('提交成功', {icon: 1}, function (index) {
                            window.parent.location.reload();
                        });
                    } else {
                        layer.alert(msg.msg, {icon: 2});
                    }
                }
            });
            return false;
        });

        /**打开layer窗口**/
        var active = {
            setTop: function (othis) {
                var title = othis.data('title') ? ' - ' + othis.data('title') : '';
                //多窗口模式，层叠置顶
                layer.open({
                    type: 2 //此处以iframe举例
                    , title: othis.text() + title
                    , area: [($(window).width() - 40) + 'px', ($(window).height() - 40) + 'px']
                    , offset: [ 20 , 20]
                    , content: othis.data('href')
                });
            }
        };

        $(document).on('click', '.open_layer', function () {
            var othis = $(this), method = othis.data('method');
            active[method] ? active[method].call(this, othis) : '';
        });
        /**打开layer窗口  end**/

        /**点击分页跳转**/
        $(document).on('click', '.layui-laypage a', function () {
            var page = $(this).data('page');
            get_list(page);
        });

    });
});

// 获取分页数据
function get_list(page) {
    var index = layer.load();
    var $ = layui.jquery;
    var url = window.location.href;
    $.get(url + '?page=' + page, function (msg) {
        if (msg.code == 200) {
            $("#data_list").html(msg.tpl);
            //关闭
            layer.close(index);
        } else {
            layer.alert(msg.msg, {
                title: '信息'
            });
        }
    }, 'json');
}